package com.shop.service.impl;

import com.shop.dto.ProductDTO;
import com.shop.entity.Product;
import com.shop.entity.Shop;
import com.shop.exception.ResourceNotFoundException;
import com.shop.repository.ProductRepository;
import com.shop.repository.ShopRepository;
import com.shop.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    
    @Autowired
    private ProductRepository productRepository;
    
    @Autowired
    private ShopRepository shopRepository;
    
    @Override
    public Product createProduct(ProductDTO productDTO) {
        Shop shop = shopRepository.findById(productDTO.getShopId())
            .orElseThrow(() -> new ResourceNotFoundException("Shop not found"));
            
        Product product = new Product();
        BeanUtils.copyProperties(productDTO, product);
        product.setShop(shop);
        product.setStatus(1);
        
        return productRepository.save(product);
    }
    
    @Override
    public Page<Product> getProductsByShop(Long shopId, Pageable pageable) {
        return productRepository.findByShopIdAndStatus(shopId, 1, pageable);
    }
}